
import sys
from datetime import datetime, timedelta
import boto3

total_cases = int(sys.argv[1])
passed_cases = int(sys.argv[2])
failed_cases = int(sys.argv[3])
skipped_cases = int(sys.argv[4])
broken_cases = int(sys.argv[5])
report_address = sys.argv[6]
email_title = sys.argv[7]
alarm_title = sys.argv[8]


class SendMail:
    """发送mail邮件"""

    def __init__(self, total, passed, failed, skipped, broken, report_addr, title, alarm_t):
        self.total = total
        self.passed = passed
        self.failed = failed
        self.skipped = skipped
        self.broken = broken
        self.passing_rate = round((self.passed / (self.total - self.skipped)) * 100, 2)
        self.report_addr = report_addr
        self.title = title
        self.alarm_title = alarm_t
        self.cur_datetime = datetime.strftime(datetime.now() + timedelta(hours=16), "%Y/%m/%d %H:%M:%S")
        self.style = '''
        <style>
        .table table {
            width:100%;
            margin:15px 0}
        .table th {
            background-color:#32AAFF;
            color:#FFFFFF}
        .table,.table th,.table td{
            font-size:0.95em;
            text-align:center;
            padding:4px;
            border:1px solid #dddddd;
            border-collapse:collapse}
        </style>
        '''

        self.html_content = f'''
        <html>
        <head>
            <meta charset="UTF-8">
            <title>TestReport</title>
            {self.style}
        </head>
        <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
            <div width="95%" cellpadding="0" cellspacing="0" 
                style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                <div>
                    本邮件由系统自动发出，无需回复！
                </div>
                <div>
                    <hr size="2" width="100%" />
                    <h4><font color="#0B610B">测试结果</font></h4>
                    <table class=table>
                        <tr>
                            <th>Total of cases</th>
                            <th>Pass</th>
                            <th>Fail</th>
                            <th>Broken</th>
                            <th>Passing rate</th>
                        </tr>
                        <tr>
                            <td>{self.total - self.skipped}</td>
                            <td style="color:#66cc00">{self.passed}</td>
                            <td style="color:#ff3333">{self.failed}</td>
                            <td style="color:#ffeb3b">{self.broken}</td>
                            <td>{self.passing_rate}%
                            </td>
                        </tr>
                    </table>
                    <hr size="2" width="100%" />
                    <a style="color:#ff3333" href="{self.report_addr}">→在线报告预览：点我！←</a>
                </div>
            </div>
        </body>
        </html>
        '''

    def send_gmail(self, to_user, title, content_type='Html'):
        ''' 发送gmail邮件
            :param to_user: 对方邮箱
            :param title: 邮件标题
            :param content_type: 内容格式
        '''
        client = boto3.client('ses', region_name="us-east-1", aws_access_key_id='AKIAXQ5JHWBV7LIGXI5Y',
                              aws_secret_access_key='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')
        client.send_email(
            Destination={
                'ToAddresses': to_user,
            },
            Message={
                'Body': {
                    f'{content_type}': {
                        'Data': self.html_content,
                    },
                },
                'Subject': {
                    'Data': f'{title}{self.cur_datetime}',
                },
            },
            Source="noreply@i9i8.com",
        )

    def email_trigger(self):
        self.send_gmail(to_user=('autotestconsignee@i9i8.com',), title=self.title)
        if '100' not in str(self.passing_rate) and 'P0' in self.alarm_title:
            self.send_gmail(to_user=['alarmbackup@i9i8.com', 'autotestconsignee@i9i8.com', 'alarm_az@i9i8.com'],
                            title=self.alarm_title,
                            content_type='Html'
                            )


SendMail(total_cases, passed_cases, failed_cases, skipped_cases, broken_cases, report_address, email_title, alarm_title
         ).email_trigger()
